/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.bean;

import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.service.AttributeTypeService;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.codehaus.jettison.json.JSONException;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author U
 */
@ManagedBean(name = "attributebean")
@ViewScoped
public class AttributeTypeBean implements Serializable {

    private String[] flags;
    private DataModel<AttributeType> attributeTypes;
    private AttributeTypeService attributeTypeService;
    private AttributeType attributeType;
    String confirmadd = "start";
    private List<AttributeType> ats;

    public String getConfirmadd() {
        return confirmadd;
    }

    public void setConfirmadd(String confirmadd) {
        this.confirmadd = confirmadd;
    }

    public AttributeTypeBean() {

        flags = new String[2];
        flags[0] = "Yes";
        flags[1] = "No";
        try {
            flags = new String[2];
            flags[0] = "Yes";
            flags[1] = "No";
            attributeTypeService = new AttributeTypeService();

            List<AttributeType> allAttribute = attributeTypeService.getAllAttribute();
            ats=allAttribute;
            attributeTypes = new ListDataModel(allAttribute);
            attributeType = new AttributeType();
        } catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }

    }

    public void showMessage() {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error Message", "This Attribute Already Added");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }

    public void showMessageConfirmation() {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Confirm", "Done");
        RequestContext.getCurrentInstance().showMessageInDialog(message);
    }

    public void onEdit(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Attribute Edited", ((AttributeType) event.getObject()).getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        AttributeType attribute = (AttributeType) event.getObject();
        System.out.println("atatta====-----==tatattatata"+attribute);
       attributeTypes = new ListDataModel<AttributeType>(new ArrayList<AttributeType>());
         String xx = attributeTypeService.UpdateAttribute(attribute.getId(), attribute.getName(), attribute.getIsNumber());
            if (xx.equals("Done")) {
            try {
                //  showMessageConfirmation();
                ats.clear();
                ats=attributeTypeService.getAllAttribute();
                for (AttributeType at : ats) {
                    System.out.println("atattatatattatata"+at);
                }
                attributeTypes = new ListDataModel(ats);
            } catch (JSONException ex) {
                Logger.getLogger(AttributeTypeBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
    }

    public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Attribute Cancelled", ((AttributeType) event.getObject()).getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void info() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Attribute Added Succesfully"));
    }

    public void error() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Attribute Already Exist!!"));
    }

    public void addAttributeType() {
        try {
            if (attributeType.getName().equals("")) 
            {
                   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Plz Enter Attribute Name"));
            } else {
                String x = attributeTypeService.insertNewAttribute(attributeType);
                if (x.equals("valid")) {
                    List<AttributeType> allAttribute = attributeTypeService.getAllAttribute();
                    attributeTypes = new ListDataModel(allAttribute);
                    ats=allAttribute;
                    info();
                } else {
                    error();
                }
            }
        }catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteBean() {
        try {
            AttributeType attribute = attributeTypes.getRowData();
            attributeTypeService.RemoveAttribute(attribute.getId());
            ats=attributeTypeService.getAllAttribute();
            attributeTypes = new ListDataModel(ats);
        } catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void updateBean() {
        try {
            AttributeType attribute = attributeTypes.getRowData();
            String xx = attributeTypeService.UpdateAttribute(attribute.getId(), attribute.getName(), attribute.getIsNumber());
            if (xx.equals("Done")) {
              //  showMessageConfirmation();
                 ats=attributeTypeService.getAllAttribute();
                attributeTypes = new ListDataModel(ats);
            }
        } catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }

    }

    public DataModel<AttributeType> getAttributeTypes() {
        return attributeTypes;
    }

    public void setAttributeTypes(DataModel<AttributeType> attributeTypes) {
        this.attributeTypes = attributeTypes;
    }

    public AttributeTypeService getAttributeTypeService() {
        return attributeTypeService;
    }

    public void setAttributeTypeService(AttributeTypeService attributeTypeService) {
        this.attributeTypeService = attributeTypeService;
    }

    public AttributeType getAttributeType() {
        return attributeType;
    }

    public void setAttributeType(AttributeType attributeType) {
        this.attributeType = attributeType;
    }

    public String[] getFlags() {
        return flags;
    }

    public void setFlags(String[] flags) {
        this.flags = flags;
    }

    public void viewadd() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("addAttribute.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public List<AttributeType> getAts() {
        return ats;
    }

    public void setAts(List<AttributeType> ats) {
        this.ats = ats;
    }
}
